//携带旧邮箱信息
$(function(){
	var model;
	$.ajax({
		url : '/web-adv/gainEmail',
		data : model,
		type : 'post',
		success : function(data){
			$("#oldEmail").val(data.email);
		},
		error : function(){
			console.log("err");
		}
	})
})
 /**获取邮箱验证码**/
 $("#emailCode").click (function(){
	var email = $("#newEmail").val();
	$.ajax({
		url : "/web-adv/sendEmailVerCode",
		type : "POST",
		dataType : "json",
		data : {
			"newEmail" : email
		},
		success : function(json) {
			if (json.status == 1) { // 1 邮件发送成功
				//倒计时
				settime();
			} else if (json.status == -1) { // -1 邮件发送失败
				$("#error_tip2").html(json.error);
			}
		},
		error : function(json) {
		}
	});
 });
var countdown;
var timer=null;
/**倒计时**/
function settime(){
	 countdown=60;
	 $("#emailCode").attr("disabled",true);
	 timer=setInterval(function(){
		countdown--;
		$("#emailCode").html(countdown+'s');
		if (countdown <= 0) {
			$("#emailCode").html("重新获取");
			clearInterval(timer);
			$("#emailCode").attr("disabled",false);
		} 
	 }, 1000);
};
function emailpattern(){
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var email=$("#newEmail").val();
	if(reg.test(email)){
		return true;
	}else{
		return false;
	}
}
$("#newEmail").on("blur",function(){
	var email=$("#newEmail").val();
	var oldEmail=$("#oldEmail").val();
	if(!email){
		$("#error_tip2").html("*请输入邮箱");
	}else if(!emailpattern()){
		$("#error_tip2").html("*请输入有效邮箱");
	}else if(email==oldEmail){
		$("#error_tip2").html("*该邮箱已被注册");				
	}else{
		$("#error_tip2").html("");
	}
});
  /**更改邮箱**/
  $("#nextStep").click(function(){
     var newEmail=$("#newEmail").val();
 	 var code= $("#verCode").val();
	 var param={"newEmail":newEmail,"verCode":code};
	 if(!code){
		 $("#error_tip5").html("*请输入验证码");
	 }else{
		 $.ajax({
	 			url:"/web-adv/checkNewEmail",
				type:"POST",
	 			dataType:"json",
	 			data:param,
	 		    success : function(json) {
	 		    	if(json.status==1){ //验证码正确 ，跳转页面 
	 		    		 var path=escape('selectedNav=0&navPos=4');
	                     window.location.href = "/web-adv/advWeb/views/userCenter/accountSecurity/modifyMailSuccess.html?"+path;
	 		    	}else if(json.status==-1){ // -1 验证码输入错误
	 		    		$("#error_tip5").html(json.error);  	
	 		    	}
	 		    },
	 	        error: function(json){
	 	        }     
	 	   });
	  }
  });
	
	
	
  